<?php

namespace App\Http\Controllers\Admin;

use App\Models\Qh_School;
use App\Models\Qh_SchoolArticle;
use App\Models\TQ_Config;
use Arr;
use Illuminate\Http\JsonResponse;

class ConfigController extends AdminBaseController
{
    /**
     * 获取首页配置
     * @return JsonResponse|object
     */
    public function getHomeConfig() {
        $config = TQ_Config::where('key', 'home_config')->first();
        if (!$config) {
            $config = new TQ_Config(['key' => 'home_config', 'value' => json_encode([
                'headerBackgroundImage' => null,
                'banner' => [],
                'components' => []
            ])]);
            $config->save();
        }

        // 排序
        $valueJson = json_decode($config->value);
        $componentsSorts = array();
        foreach ($valueJson->components as $key => $row) {
            $componentsSorts[$key] = $row->sort;
        }
        array_multisort($componentsSorts, SORT_ASC, $valueJson->components);

        return $this->restful($valueJson);
    }

    public function saveHomeConfig() {
        $params = request()->validate([
            'headerBackgroundImage' => 'bail|nullable|string',
            'banner' => 'bail|nullable|array',
            'components' => 'bail|nullable|array'
        ], request()->all());

        $result = TQ_Config::where('key', 'home_config')->update([
            'value' => json_encode(Arr::only($params, ['headerBackgroundImage', 'banner', 'components']))
        ]);

        // 删掉缓存
        \Cache::forget('config:homePageConfig');

        return $this->restful($result);
    }

    public function getWangkeConfig() {
        $config = TQ_Config::where('key', 'wangke_config')->first();
        if (!$config) {
            $config = new TQ_Config(['key' => 'wangke_config', 'value' => json_encode([
                'banner' => [],
                'textList' => null,
                'longBanner' => [],
                'components' => [],
                'headerBackgroundImage' => null,
                'cheatImage' => null,
                'pageMode' => 1
            ])]);
            $config->save();
        }

        $valueJson = json_decode($config->value);
        return $this->restful($valueJson);
    }

    public function saveWangkeConfig() {
        $params = request()->validate([
            'banner' => 'bail|nullable|array',
            'textList' => 'bail|nullable|numeric',
            'longBanner' => 'bail|nullable|array',
            'components' => 'bail|nullable|array',
            'headerBackgroundImage' => 'bail|nullable|string',
            'pageMode' => 'bail|nullable|in:1,2',
            'cheatImage' => 'bail|nullable|string',
        ], request()->all());

        $result = TQ_Config::where('key', 'wangke_config')->update([
            'value' => json_encode(Arr::only($params, ['banner', 'textList', 'longBanner', 'components', 'headerBackgroundImage', 'pageMode', 'cheatImage'])),
        ]);
        return $this->restful($result);
    }

    public function getKaoshiConfig() {
        $config = TQ_Config::where('key', 'kaoshi_config')->first();
        if (!$config) {
            $config = new TQ_Config(['key' => 'kaoshi_config', 'value' => json_encode([
                'components' => []
            ])]);
            $config->save();
        }

        // 排序
        $valueJson = json_decode($config->value);
        $componentsSorts = array();
        foreach ($valueJson->components as $key => $row) {
            $componentsSorts[$key] = $row->sort;
        }
        array_multisort($componentsSorts, SORT_ASC, $valueJson->components);

        return $this->restful($valueJson);
    }

    public function saveKaoshiConfig() {
        $params = request()->validate([
            'components' => 'bail|nullable|array'
        ], request()->all());

        $result = TQ_Config::where('key', 'kaoshi_config')->update([
            'value' => json_encode(Arr::only($params, ['components']))
        ]);

        return $this->restful($result);
    }

    public function getSchoolConfig() {
        $config = TQ_Config::where('key', 'school_config')->first();
        if (!$config) {
            $config = new TQ_Config(['key' => 'school_config', 'value' => json_encode([
                'banner' => [],
                'notice' => null,
            ])]);
            $config->save();
        }

        // 排序
        $valueJson = json_decode($config->value);

        return $this->restful([
            'data' => $valueJson
        ]);
    }

    public function saveSchoolConfig() {
        $params = request()->validate([
            'banner' => 'bail|nullable|array',
            'notice' => 'bail|nullable|integer'
        ], request()->all());

        $result = TQ_Config::where('key', 'school_config')->update([
            'value' => json_encode(Arr::only($params, ['banner', 'notice']))
        ]);

        return $this->restful($result);
    }

    public function getGlobalConfig() {
        $config = TQ_Config::where('key', 'global_config')->first();
        if (!$config) {
            $config = new TQ_Config(['key' => 'global_config', 'value' => json_encode([
                'shareTitle' => null,
                'shareImage' => null,
                'payment_enable' => true,
                'alert_ads_enable' => false,
                'alert_ads_show_rate' => null,
                'alert_ads_image' => null,
                'alert_ads_jump_target' => null,
                'alert_ads_jump_type' => null,
                'alert_ads_jump_params' => null,
                'course_price_enable' => true,
                'wx_video' => [
                    'channelEnabled' => false,
                    'liveEnabled' => false,
                    'finderUserName' => null,
                ]
            ])]);
            $config->save();
        }

        return $this->restful(json_decode($config->value));
    }

    public function saveGlobalConfig() {
        $params = request()->validate([
            'data' => 'bail|nullable',
        ], request()->all());

        $config = TQ_Config::where('key', 'global_config')->update([
            'value' => json_encode($params['data'])
        ]);

        return $this->restful();
    }


    public function getDocumentConfig() {
        $config = TQ_Config::where('key', 'document_config')->first();
        if (!$config) {
            $config = new TQ_Config(['key' => 'document_config', 'value' => json_encode([
                'card1_image' => null,
                'card1_jump_type' => null,
                'card1_jump_target' => null,
                'card1_jump_params' => null,
                'card2_image' => null,
                'card2_jump_type' => null,
                'card2_jump_target' => null,
                'card2_jump_params' => null,
            ])]);
            $config->save();
        }

        return $this->restful(json_decode($config->value));
    }

    public function saveDocumentConfig() {
        $params = request()->validate([
            'data' => 'bail|nullable',
        ], request()->all());

        $config = TQ_Config::where('key', 'document_config')->update([
            'value' => json_encode($params['data'])
        ]);

        return $this->restful();
    }
}
